package soccer.model.team;

import java.util.List;
import java.util.Map;

import soccer.model.player.Player;
import soccer.utility.db.dbUtility;


    public class TeamDAOImpl implements TeamDAO{
     public int insertTeam (Team newTeam){
    	 StringBuffer query  = new StringBuffer();
    	 query.append("INSERT INTO ");
    	 query.append(TeamConstant.TABLENAME);
    	 query.append(" (teamKodu , teamName, establishDate , city)");
    	 query.append(" VALUES (?,?,?,?)");
    	 
    	 Object[] params = new Object[]{newTeam.getTeamCode(), newTeam.getTeamName(), 
    			 newTeam.getEstablishDate(),newTeam.getCity()};
    	 dbUtility db = new dbUtility();
    	 int rows = db.update(query.toString(), params);
    	 return rows;
     }
     
     public void addPlayerToTeam( int playerId , int teamId){
    	 StringBuffer  query = new StringBuffer();
    	 query.append("UPDATE ");
    	 query.append("player ");
    	 query.append("SET teamId = ? ");
    	 query.append("WHERE player.playerId = ? ");
    	 
    	 Object[] params = new Object[]{teamId , playerId};
    	 dbUtility db = new dbUtility();
    	 db.update(query.toString(), params);	 
     }
     
     public List<Map> searchTeamByName(String name){
    	  List<Map> teamList = null;
    	  StringBuffer query = new StringBuffer();
    	  query.append("SELECT * FROM ");
    	  query.append(TeamConstant.TABLENAME);
    	  query.append(" WHERE teamName = ? ");
    	  
    	  Object [] params = new Object[]{name};
    	  dbUtility db = new dbUtility();
    	  teamList = db.execute(query.toString(), params);
    	  return teamList;
     }
     public List<Map>  searchTeamByCity(String city){
    	 List<Map> teamList = null;
   	     StringBuffer query = new StringBuffer();
   	     query.append("SELECT * FROM ");
   	     query.append(TeamConstant.TABLENAME);
   	     query.append(" WHERE city = ? ");
   	  
   	     Object [] params = new Object[]{city};
   	     dbUtility db =  dbUtility.getInstance();
   	     teamList = db.execute(query.toString(), params);
   	     return teamList;
     }
     public List<Map> getTeams(){
    	 List<Map> teamsList = null;
    	 String query = "SELECT * FROM "+TeamConstant.TABLENAME;
    	 dbUtility db = dbUtility.getInstance();
    	 teamsList = db.execute(query);
    	 return teamsList;
     }
     public List<Map> getTeams(int teamCode){
    	 List<Map> teamList = null;
    	 String query = "SELECT * FROM team WHERE teamKodu = "+teamCode;
    	 dbUtility db = dbUtility.getInstance();
    	 teamList = db.execute(query);
    	 return teamList;
     }
}     
